package com.hd.crdao.activity;

import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseActivity;
import com.hd.crdao.bean.CheckOrderBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 *
 *@author Administrator
 *@date 2017/7/4 0004
 */
public class BuyDomainActivity extends BaseActivity {
    @BindView(R.id.domain_head)
    ExpandHeadView mDomainHead;
    @BindView(R.id.domaim)
    TextView mDomaim;
    @BindView(R.id.domain_price)
    TextView mDomainPrice;
    @BindView(R.id.buy)
    Button mBuy;
    @BindView(R.id.serviceagreement)
    TextView mServiceagreement;
    public String mDomainName;
    public double mDomainPrice1;
    public String mId;
    public String mBusinessid;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_buydomain;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        //设置服务条款协议的富文本
        SpannableString spannableString = new SpannableString("确认即表示同意《域名注册购买协议》");
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.RED);
        spannableString.setSpan(colorSpan,8, spannableString.length() - 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        mServiceagreement.setText(spannableString);

    }

    @Override
    protected void initListener() {
        mDomainHead.getLeftArrowLL().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        mBusinessid = SPUtils.getString("businessid", "");
        mDomainName = getIntent().getStringExtra("domainName");
        mDomainPrice1 = getIntent().getDoubleExtra("domainPrice",0.01);
        mId = getIntent().getStringExtra("id");
        mDomaim.setText(mDomainName);
        mDomainPrice.setText("￥"+ mDomainPrice1);
    }

    @OnClick({R.id.buy, R.id.serviceagreement})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.buy:
                checkOrder();
                finish();
                break;
            case R.id.serviceagreement:
                startActivity(new Intent(UIUtils.getContext(),UserAgreementActivity.class));
                break;
        }
    }

    /**
     * 调确认订单接口,获取订单号
     */
    private void checkOrder() {
        try {
            OkHttpUtils.post().url(Constants.CHECKORDER_URL).addParams("domainId", mId).addParams("carBusinessId", mBusinessid)
                    .addParams("sign", Constants.SIGN).build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    MyToast.show("网络开了个小差");
                }

                @Override
                public void onResponse(String response, int id) {
                    ALog.d("请求成功...........");
                    Gson gson = new Gson();
                    CheckOrderBean checkOrderBean = gson.fromJson(response, CheckOrderBean.class);
                    String success = checkOrderBean.getSuccess();
                    String message = checkOrderBean.getMessage();
                    String plOrder = checkOrderBean.getPlOrder();
                    SPUtils.saveString("plOrder",plOrder);
                    if(TextUtils.equals("00",success)){
                        Intent intent = new Intent(UIUtils.getContext(),OrderCheckActivity.class);
                        intent.putExtra("domainName",mDomainName);
                        intent.putExtra("domainPrice",mDomainPrice1);
                        intent.putExtra("id",mId);
                        intent.putExtra("plOrder",plOrder);
                        startActivity(intent);
                        finish();
                    }else {
                        MyToast.show(message);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
